package com.ahpu.jym.messagealert.ui;

import java.io.IOException;

import com.ahpu.jym.messagealert.tools.OptionsData;
import com.ahpu.jym.messagealert.tools.Tools;

import resource.localResource;
import net.rim.device.api.ui.MenuItem;
import net.rim.device.api.ui.Ui;
import net.rim.device.api.ui.UiApplication;
import net.rim.device.api.ui.UiEngine;
import net.rim.device.api.ui.component.Dialog;
import net.rim.device.api.ui.component.LabelField;
import net.rim.device.api.ui.component.Menu;
import net.rim.device.api.ui.component.ObjectChoiceField;
import net.rim.device.api.ui.component.SeparatorField;
import net.rim.device.api.ui.container.MainScreen;
import net.rim.device.api.util.StringProvider;

/**
 * A class extending the MainScreen class, which provides default standard
 * behavior for BlackBerry GUI applications.
 */
public final class SettingScreen extends MainScreen {

	private ObjectChoiceField _status;
	private ObjectChoiceField _mailstatus;
	private ObjectChoiceField _inHolster;

	private ObjectChoiceField _backlight;
	private ObjectChoiceField _popCall;
	private ObjectChoiceField _showButton;
	private ObjectChoiceField _connectVibrate;
	private ObjectChoiceField _disconnectVibrate;
	// private ObjectChoiceField _show139;
	private ObjectChoiceField _gomessages;

	private String[] Choices = { Tools.getResString(localResource.Yes),
			Tools.getResString(localResource.No) };

	private OptionsData _data = OptionsData.load();
	private MenuItem previewItem;
	private MenuItem aboutItem;

	public SettingScreen() {

		// new MailListening();
		_status = new ObjectChoiceField(
				Tools.getResString(localResource.Isopen), Choices,
				_data.getStatus());
		_gomessages = new ObjectChoiceField(
				Tools.getResString(localResource.GoMeaage), Choices,
				_data.getGomessages());
		_mailstatus = new ObjectChoiceField(
				Tools.getResString(localResource.MailAlert), Choices,
				_data.getMailstatus());
		_inHolster = new ObjectChoiceField(
				Tools.getResString(localResource.Hander_pop), Choices,
				_data.getHander());

		_popCall = new ObjectChoiceField(
				Tools.getResString(localResource.Call_pop), Choices,
				_data.getActiveCall());

		_showButton = new ObjectChoiceField(
				Tools.getResString(localResource.ShowButton), Choices,
				_data.getShowButton());
		_backlight = new ObjectChoiceField(
				Tools.getResString(localResource.Backlight), Choices,
				_data.getBacklight());

		LabelField callField = new LabelField(
				Tools.getResString(localResource.Call_option));

		_connectVibrate = new ObjectChoiceField(
				Tools.getResString(localResource.Conn_Vibrate), Choices,
				_data.getConnectVibrate());

		_disconnectVibrate = new ObjectChoiceField(
				Tools.getResString(localResource.DisConn_Vibrate), Choices,
				_data.getDisconnectVibrate());

		previewItem = new MenuItem(new StringProvider(
				Tools.getResString(localResource.Preview)), 101, 100) {
			public void run() {
				UiEngine ui = Ui.getUiEngine();
				ui.pushGlobalScreen(
						new MessageAlertDailog(Tools
								.getResString(localResource.Test_address),
								Tools.getResString(localResource.Test_Message),
								_showButton.getSelectedIndex() == 0), 10,
						UiApplication.GLOBAL_QUEUE);
			};
		};

		aboutItem = new MenuItem(new StringProvider(
				Tools.getResString(localResource.About)), 100, 100) {
			public void run() {
				Dialog.alert(Tools.getResString(localResource.About_context));
			};
		};

		add(_status);
		add(_mailstatus);
		add(_inHolster);
		add(_popCall);
		add(_showButton);
		add(_gomessages);
		add(_backlight);

		// add(_show139);
		add(new SeparatorField());
		add(callField);
		add(new SeparatorField());
		add(_connectVibrate);
		add(_disconnectVibrate);

		setTitle(Tools.getResString(localResource.Title));

	}

	protected void makeMenu(Menu menu, int instance) {
		// TODO Auto-generated method stub
		menu.add(aboutItem);
		menu.add(previewItem);
		super.makeMenu(menu, instance);
	}

	public void save() throws IOException {
		// TODO Auto-generated method stub
		_data.setActiveCall(_popCall.getSelectedIndex());
		_data.setConnectVibrate(_connectVibrate.getSelectedIndex());
		_data.setDisconnectVibrate(_disconnectVibrate.getSelectedIndex());
		_data.setShowButton(_showButton.getSelectedIndex());
		_data.setHander(_inHolster.getSelectedIndex());
		_data.setStatus(_status.getSelectedIndex());
		_data.setGomessages(_gomessages.getSelectedIndex());
		// _data.setShow139(_show139.getSelectedIndex());
		_data.setMailstatus(_mailstatus.getSelectedIndex());
		_data.setBacklight(_backlight.getSelectedIndex());
		_data.commit();
		super.save();
	}

}
